package cn.waps;

import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class WapsFloat implements OnClickListener, OnTouchListener {
	Context context;
	WindowManager wm;
	WindowManager.LayoutParams wmParams;
	Rect rect = new Rect();
	float downX, downY;
	ImageView imeView;
	
	
	
	
	public WapsFloat(Context context) {
		super();
		this.context = context;
		init();
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		DisplayMetrics dm = v.getResources().getDisplayMetrics();
		int scnW = dm.widthPixels, scnH = dm.heightPixels, stbH = 20;
		
		//获取窗口可见大小
		v.getGlobalVisibleRect(rect);
		v.getLocalVisibleRect(rect);
		
		
		// 2、获取相对屏幕的坐标，即以屏幕左上角为原点 。y轴坐标= y（获取到屏幕原点的距离）-状态栏的高度
		float x = event.getRawX();
		float y = event.getRawY(); // statusBarHeight是系统状态栏的高度
		
		// 3、处理触摸移动
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN: { // 捕获手指触摸按下动作
			downX = x;
			downY = y;
			break;
		}

		case MotionEvent.ACTION_MOVE: { // 捕获手指触摸移动动作
			float moveX = x - downX;
			float moveY = y - downY;
			
			if (Math.abs(moveX) > 5 || Math.abs(moveY) > 5) {
			}

			wmParams.x += (x - downX);
			wmParams.y += (y - downY);
			downX = x;
			downY = y;

			if (wmParams.x < 0)
				wmParams.x = 0;
			else if (wmParams.x + rect.width() > scnW)
				wmParams.x = scnW - rect.width();

			if (wmParams.y < stbH)
				wmParams.y = stbH;
			else if (wmParams.y + rect.height() > scnH)
				wmParams.y = scnH - rect.height();

			wm.updateViewLayout(imeView, wmParams);
			break;
		}

		case MotionEvent.ACTION_UP: { // 捕获手指触摸离开动作
			wm.updateViewLayout(imeView, wmParams);
			break;
		}
		}
		
		return false;
	}

	@Override
	public void onClick(View v) {
		// 显示推荐列表（综合）
		AppConnect.getInstance(context).showOffers(WapsApplication.top);
		
		// 设置插屏广告无数据时的回调监听（该方法必须在showPopAd之前调用）
		AppConnect.getInstance(context).setPopAdNoDataListener(new AppListener() {

			@Override
			public void onPopNoData() {
				Log.i("debug", "插屏广告暂无可用数据");
			}
		});
		
		// 显示插屏广告
		AppConnect.getInstance(context).showPopAd(WapsApplication.top);
	}
	
	void init() {
		DisplayMetrics dm = context.getResources().getDisplayMetrics();
		int btnH = Math.round(50 * dm.density);
		int resId = context.getResources().getIdentifier("waps_icon" , "drawable", context.getPackageName());
		
		imeView = new ImageView(context);
		imeView.setScaleType(ScaleType.CENTER);
		imeView.setImageResource(resId);
		imeView.setOnClickListener(this);
		imeView.setOnTouchListener(this);
		
		// 1、获取WindowManager对象
		wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		// 2、设置LayoutParams(全局变量）相关参数
		wmParams = new WindowManager.LayoutParams();

		// 3、设置相关的窗口布局参数 （悬浮窗口效果）
//		if(PackageManager.PERMISSION_GRANTED == context.checkCallingOrSelfPermission("android.permission.SYSTEM_ALERT_WINDOW")) {
//			wmParams.type = WindowManager.LayoutParams.TYPE_PHONE; // 设置window type
//		} else {
//			wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL; // 设置window type
//		}
		wmParams.type = WindowManager.LayoutParams.TYPE_PHONE; // 设置window type
		wmParams.format = PixelFormat.RGBA_8888; // 设置图片格式，效果为背景透明

		// 4、设置Window flag == 不影响后面的事件 和 不可聚焦
		wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
		/*
		 * 注意，flag的值可以为： LayoutParams.FLAG_NOT_TOUCH_MODAL 不影响后面的事件
		 * LayoutParams.FLAG_NOT_FOCUSABLE 不可聚焦 LayoutParams.FLAG_NOT_TOUCHABLE
		 * 不可触摸
		 */
		// wmParams.flags =
		// LayoutParams.FLAG_NOT_TOUCHABLE|LayoutParams.FLAG_NOT_FOCUSABLE;


		// 5、 调整悬浮窗口至左上角，便于调整坐标
		wmParams.gravity = Gravity.LEFT | Gravity.TOP;
		
		wmParams.height = LayoutParams.WRAP_CONTENT;
	}
	
	public void show() {
		DisplayMetrics dm = context.getResources().getDisplayMetrics();
		
		// 以屏幕左上角为原点，设置x、y初始值
		wmParams.x = 0;
		wmParams.y = (dm.heightPixels - wmParams.height)/2;
		
		// 6、设置悬浮窗口长宽数据
		wmParams.width = LayoutParams.WRAP_CONTENT;
		
		wm.addView(imeView, wmParams);
	}
}
